package ca.uwindsor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import android.app.AlertDialog;
import android.app.ExpandableListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;

public class ViewMarks extends ExpandableListActivity {

	List<String> courseList = new ArrayList<String>(); //used to store child data.
	HashMap<String, String> idArray = new HashMap<String, String>(); //stores the id of the table row to access later
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//set window title to sem name
		//requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.view_marks);
		//getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.summary);
		//TextView lblSemName = (TextView)findViewById(R.id.lblSemName);
		//lblSemName.setText(Common.globalTerm);
		//TODO:
		//Summary here
		
		 //Create Expandable lists
		SimpleExpandableListAdapter expListAdapter =
				new SimpleExpandableListAdapter(
					this,
					createGroupList(),	// groupData describes the first-level entries
					R.layout.child_row,	// Layout for the first-level entries
					new String[] { "course" },	// Key in the groupData maps to display
					new int[] { R.id.lblDay },		// Data under "Day" key goes into this TextView
					createChildList(),	// childData describes second-level entries
					R.layout.child_row,	// Layout for second-level entries
					new String[] { "marks", "weightage" },	// Keys in childData maps to display
					new int[] { R.id.lblDay, R.id.lblTime }	// Data under the keys above go into these TextViews
				);
			setListAdapter( expListAdapter );
		
		
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}


	
	//Creates the Course lists and returns the list
	private List<HashMap<String, String>> createGroupList() {
		
		final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
		db.setDATABASE_TABLE("courses");
		db.open();
		
		Cursor ret = db.getEntry("sem_name",Common.globalTerm );
		
		ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();
		 
		if(ret.moveToFirst()){
			do{		
				//populate the list
				HashMap<String, String> m = new HashMap<String, String>();
			    m.put( "course",ret.getString(2)+"  "+ret.getString(3) );
				result.add( m );
				
				
				//create child list
				//Store all the courses in a list. Later passed to createChildList to create the sub list
				
				courseList.add(ret.getString(2));
				
				
			}while(ret.moveToNext());
		}
		db.close();
		
		  return (List<HashMap<String, String>>)result;
	    }
	
	//Create the child list. Passed in the course list
	private List<ArrayList<HashMap<String, String>>> createChildList() {
		final MyDBAdapter db = new MyDBAdapter(getApplicationContext());
		db.setDATABASE_TABLE("marks");
		db.open();
		
		Iterator<String> listiter = courseList.iterator();
		
		ArrayList<ArrayList<HashMap<String, String>>> result = new ArrayList<ArrayList<HashMap<String, String>>>();
		
		
		while(listiter.hasNext()){
		
		Cursor ret_sch = db.getEntry("course_code",listiter.next() );
		ArrayList<HashMap<String, String>> secList = new ArrayList<HashMap<String, String>>(); //VOILA!!!!
		
		if(ret_sch.moveToFirst()){
			do{
				
				 	HashMap<String, String> child = new HashMap<String, String>();
					child.put( "marks", ret_sch.getString(2));
				    child.put( "weightage", ret_sch.getString(4)+"%");
					secList.add( child );
					idArray.put((result.size())+""+(secList.size()-1), ""+ret_sch.getInt(0));
				
			}while(ret_sch.moveToNext());
			 result.add( secList ); 
		}else{ 
			//if no schedule for course exists alert the user.
			HashMap<String, String> child = new HashMap<String, String>();
			child.put( "marks", "No marks exists for this course.");
		    child.put( "weightage", "" );
			secList.add( child );
			result.add( secList ); 
		}
		ret_sch.close();
	//add sub list to main list
		}
		 db.close();
		 
		return (List<ArrayList<HashMap<String, String>>>) result;
	  }
	
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View v,  int groupPosition,
	         int childPosition, long id) {
	    // use groupPosition and childPosition to locate the current item in the adapter
		
		//String groupName =(String)mAdapter.getGroup(groupPosition);
		
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		
		
		final ExpandableListAdapter mAdapter = parent.getExpandableListAdapter();

		//alert.setMessage(""+);
		final int inGroupPos = groupPosition;
		final int inChildPos = childPosition;
		
		
		alert.setMessage("Edit Marks?");
		

		alert.setNegativeButton("No", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
			}

		});
		
		//Give choice to edit.
		alert.setNeutralButton("Edit", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Intent intent = new Intent("ca.uwindsor.EDITMARKS");
				//send id to edit schedule
				int _id = Integer.parseInt(idArray.get(mAdapter.getGroupId(inGroupPos)+""+mAdapter.getChildId(inGroupPos, inChildPos)));
				intent.putExtra("_id",_id );
				startActivity(intent);
			}
		});
		
		
		alert.setCancelable(false);
		alert.show();
		
	
	    return true;
	}
	
	

}
